#include <stddef.h>
#include <stdexcept>

#include "../interfacesCol/HashedKey.h"
#include "../interfacesCol/IIterator.h"
#include "HashTable.h"
#include "Dictionary.h"

void Dictionary::add(IKey* k, ICollectible* val) {
    HashedKey *hashedKey = dynamic_cast<HashedKey *> (k);
    if (hashedKey == NULL)
        throw std::invalid_argument("Se esperaba una instancia de HashedKey");
    table.put(hashedKey, val);
}

bool Dictionary::member(IKey *k) {
    HashedKey *hashedKey = dynamic_cast<HashedKey *> (k);
    if (hashedKey == NULL)
        throw std::invalid_argument("Se esperaba una instancia de HashedKey");
    return table.get(hashedKey) != NULL;
}

void Dictionary::remove(IKey *k) {
    HashedKey *hashedKey = dynamic_cast<HashedKey *> (k);
    if (hashedKey == NULL)
        throw std::invalid_argument("Se esperaba una instancia de HashedKey");
    table.remove(hashedKey);
}

ICollectible *Dictionary::find(IKey *k)
{
    HashedKey *hashedKey = dynamic_cast<HashedKey *> (k);
    if (hashedKey == NULL)
        throw std::invalid_argument("Se esperaba una instancia de HashedKey");
    return table.get(hashedKey);
}


bool Dictionary::isEmpty()
{
    return table.getSize() == 0;
}


int Dictionary::getSize()
{
    return table.getSize();
}


IIterator *Dictionary::getIterator()
{
    return table.getIterator();
}